package com.lanou3g.ptp.user.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.lanou3g.ptp.entity.User;
import com.lanou3g.ptp.user.mapper.UserMapper;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.ptp.user.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;


@Service(timeout = 5000)
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;
    @Value("${ptp.password.salt}")
    private String PASSWORD_SALT;

    @Override
    public User userById(Long id) {
        User fin = userMapper.userById(id);
        return fin;
    }

    //模糊查询
    @Override
    public List<User> userByName(String name) {
        List<User> users = userMapper.userByName("%" + name + "%");
        return users;
    }

    //通过id删除
    @Override
    public int deleteById(Long id) {
        return userMapper.deleteById(id);
    }

    //更改用户
    @Override
    public int upUser(User user) throws Exception {
        String password = user.getPassword();
        if (password != null) {
            String password1 = MD5Utils.md5(password, PASSWORD_SALT);
            user.setPassword(password1);
        }
        return userMapper.upUser(user);
    }

    //添加用户
    @Override
    public Long addUser(User user) {
        // 加盐
        String password = user.getTel().substring(5);
        String password1 = MD5Utils.md5(password, PASSWORD_SALT);
        // 把加盐后的密码 付给user
        user.setPassword(password1);
        // 把user添加进库
        int i = userMapper.addUser(user);

        return i > 0 ? user.getId() : null;
    }

    @Override
    public List<User> findByCollegeId(Long collegeId) {
        return userMapper.findByCollegeId(collegeId);

    }

    @Override
    public List<User> findByMajorId(Long majorId) {
        return userMapper.findByMajorId(majorId);
    }


}
